//
//  RHEndpointVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/5/15.
//

#import "RHEndpointVC.h"
#import "JXCategoryView.h"
#import "JXCategoryListContainerView.h"
#import "RHSearchView.h"
#import "RHEndpointListVC.h"
#import "RHDeviceFilterView.h"

@interface RHEndpointVC ()<JXCategoryListContainerViewDelegate,JXCategoryViewDelegate,JXCategoryTitleViewDataSource,QMUITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *titles;

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, strong) RHSearchView *searchView;

@property (nonatomic, strong) RHDeviceFilterView *filterView;//筛选
@end

@implementation RHEndpointVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getAllEndpointType];
}


-(void)getAllEndpointType
{
    [QMUITips showLoadingInView:self.view];
    [BaseModel startGetWithCompletionAPIKey:H_getAllEndpointListTitle argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            if ([request.responseJSONObject[@"data"] isKindOfClass:[NSArray class]]) {
                self.titles = request.responseJSONObject[@"data"];
                self.categoryView.titles = self.titles;
                [self.categoryView reloadData];
                [self.categoryView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(44);
                }];
            }else{
                [self.categoryView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(0);
                }];
            }
            
        }
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
        }];
}

- (void)setupUI{
    kWeakSelf;
//    self.titles = @[@"EMS",@"Master",@"Pack",@"PCS"].copy;
    _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    _listContainerView.scrollView.scrollEnabled = NO;
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
    self.categoryView.cellWidth = 60;
    self.categoryView.titleSelectedColor = CMainColorGreen;
    self.categoryView.titleColor = kCOLOR_HEX(@"#8B8D92");
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.cellSpacing = 30;
//    self.categoryView.contentEdgeInsetLeft = 20;
//    self.categoryView.contentEdgeInsetRight = 20;
    self.categoryView.titleLabelVerticalOffset = -10;
    [self.view addSubview:self.categoryView];
    self.categoryView.listContainer = self.listContainerView;
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(0);
        make.height.mas_equalTo(44);
    }];
    
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
     backgroundView.size = CGSizeMake(60, 3);
     backgroundView.indicatorColor = CMainColorGreen;
     backgroundView.indicatorHeight = 3;
     backgroundView.indicatorWidth = 60;
     backgroundView.verticalMargin = -10;
     backgroundView.indicatorCornerRadius = 1.5;

     _categoryView.indicators = @[backgroundView];
     
    UIView *topView = kView;
    topView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.categoryView.mas_bottom).offset(0);
        make.height.offset(45);
    }];
    //搜索栏
    _searchView = [[RHSearchView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 55)];
    _searchView.isAutoHideSearchButton = NO;
    [topView addSubview:_searchView];
    _searchView.delegate = self;
    _searchView.searchTextField.placeholder = @"请输入关键字".rh_localized;
    
    
    QMUIButton *filterBtn = [QMUIButton buttonWithImage:@"device_filter"];
    [filterBtn addTarget:self action:@selector(deviceFilterAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:filterBtn];
    [filterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(35);
        make.right.mas_offset(-8);
        make.top.equalTo(weakSelf.searchView);
    }];
    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_offset(0);
        make.right.equalTo(filterBtn.mas_left).offset(10);
    }];
    
    
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(topView.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
}

#pragma mark ---- 筛选
-(void)deviceFilterAction
{
    if (_filterView == nil) {
        kWeakSelf;
        _filterView = [[RHDeviceFilterView alloc]initWithType:2];
        _filterView.filterBlock = ^(NSDictionary * _Nonnull filterDic) {
            UIViewController *vc =  weakSelf.listContainerView.validListDict[@(weakSelf.categoryView.selectedIndex)];
            if ([vc isKindOfClass:[RHEndpointListVC class]]) {
                RHEndpointListVC *deviceVC = (RHEndpointListVC*)vc;
                deviceVC.filterDic = filterDic;
                [deviceVC searchDeviceWithKeyCode:@""];
            }
        };
    }
    
    UIViewController *vc =  self.listContainerView.validListDict[@(self.categoryView.selectedIndex)];
    if ([vc isKindOfClass:[RHEndpointListVC class]]) {
        RHEndpointListVC *deviceVC = (RHEndpointListVC*)vc;
        _filterView.filterDic = deviceVC.filterDic;
    }
    
    [_filterView showInWindowAnimate];
    
}


#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    kWeakSelf;
   
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return  self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
   
    RHEndpointListVC *listVC = [[RHEndpointListVC alloc]init];
    listVC.endpointCode = [self.titles safeObjectAtIndex:index];
    return listVC;
}
#pragma mark - JXCategoryListContentViewDelegate
/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}
@end
